<template>
  <div class="login-box">
    <div class="login-wrapper">
      <img src="../../assets/images/login-left.jpg" alt="">
      <div class="content-wrapper">
        <div class="title">账号注册</div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
          class="form-wrapper"
        >
          <el-form-item label="用户名" prop="username">
            <el-input 
              v-model="ruleForm.username" 
              type="text"
              placeholder="请输入学号或者手机号"
              autocomplete="off" 
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              placeholder="请设置密码"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="password1">
            <el-input 
              v-model="ruleForm.password1" 
              type="password"
              placeholder="请再次输入密码"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否管理员">
            <el-switch v-model="ruleForm.isAdmin" />
          </el-form-item>
          <el-button type="primary" @click="loginHandler" :loading="loading">注 册</el-button>
        </el-form>
        <div class="register-wrapper">
          <span>已有账号！</span><span @click="$router.push('/login')">去登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { register } from '@/api';
import { validateAccountOrPwd } from '@/utils/validator';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter()

const validateUsername = (rule, value, callback) => {
  const res =  validateAccountOrPwd(value)
  if (res.valid) {
    callback()
  } else {
    callback(new Error(res.message))
  }
}

const validatePass = (rule, value, callback) => {
  const res =  validateAccountOrPwd(value)
  if (res.valid) {
    callback()
  } else {
    callback(new Error(res.message))
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== ruleForm.value.password) {
    callback(new Error("两次输入的密码不一致"))
  } else {
    callback()
  }
}

const ruleFormRef = ref(null)
const ruleForm = ref({
  username: '',
  password: '',
  password1: '',
  isAdmin: false
})
const rules = ref({
  username: [
    { validator: validateUsername, trigger: 'blur' },
  ],
  password: [
    { validator: validatePass, trigger: 'blur' },
  ],
  password1: [
    { validator: validatePass2, trigger: 'blur' }
  ]
})
const loading = ref(false)

const loginHandler = () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      console.log('submit!');
      registerHandler()
    } else {
      console.log('error submit!');
      return false;
    }
  })
  // if (!username.value || !password.value) {
  //   ElMessage.error('请输入账号密码')
  //   return
  // }
  // loading.value = true
  // axios.post('http://152.136.202.150:3000/api/auth/login', {
  //   username: username.value,
  //   password: password.value
  // }).then(res => {
  //   console.log(res);
  //   if (res.status === 200) {
  //     storage.set('token', res.data.token)
  //   }
  //   loading.value = false
  // })
}

const registerHandler = () => {
  loading.value = true
  register({
    username: ruleForm.value.username,
    password: ruleForm.value.password,
    role: ruleForm.value.isAdmin ? 'admin' : 'user'
  }).then(res => {
    console.log(res);
    loading.value = false
    ElMessage.success('注册成功')
    router.push('/login')
  })
}

</script>

<style lang="scss" scoped>
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('../../assets/images/login-bg.png') no-repeat center center / cover fixed;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-wrapper {
    width: 800px;
    height: 500px;
    display: flex;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    &:hover {
      box-shadow: 0 20px 25px -5px rgba(64, 158, 255, 0.3);
      transform: scale(1.01);
    }
    img {
      width: 400px;
      height: 400px;
      object-fit: cover;
    }
    .content-wrapper {
      flex: 1;
      background: #fafafa;
      padding: 16px;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      .title {
        font-size: 24px;
        font-weight: bold;
        color: rgb(64, 158, 255);
        text-align: center;
      }
      .form-wrapper {
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
        height: 50%;
      }
      .register-wrapper{
        display: flex;
        justify-content: center;
        align-items: center;
        span {
          font-size: 12px;
          color: #409EFF;
          &:first-child {
            color: #666666;
          }
          &:last-child {
            cursor: pointer;
          }
        }  
      }
    } 
  }
}
</style>